package io.cloud.tec.project.web;

import io.cloud.tec.project.dto.AccountDTO;
import io.cloud.tec.project.en.Account;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Welcome to reactive world ~";
    }

    @GetMapping("/flux-hello")
    public Mono<String> fluxHello() {
        return Mono.just("Welcome to reactive world ~");
    }

    /**
     * curl http://localhost:8082/flux-account
     */
    @GetMapping(value = "/flux-account", produces = {MediaType.APPLICATION_NDJSON_VALUE, MediaType.APPLICATION_NDJSON_VALUE})
    public Flux<AccountDTO> account() {
        return Flux.fromArray(new AccountDTO[]{new AccountDTO(), new AccountDTO()}).delayElements(Duration.ofSeconds(1));
    }

//    @Autowired
//    AccountRepository accountRepository;

    /**
     * loadEvents
     * @param events events
     * @return r
     */
    @PostMapping(path = "account", consumes = MediaType.APPLICATION_NDJSON_VALUE) // 1
    public Mono<Void> loadEvents(@RequestBody Flux<Account> events) {
//        return this.accountRepository.saveAll(events).then();    // 2
        return null;
    }

    /**
     * getEvents
     * @return r
     */
    @GetMapping(path = "account", produces = MediaType.APPLICATION_NDJSON_VALUE)
    public Flux<Account> getEvents() {
//        return this.accountRepository.findAll().log();
        return null;
    }


}

